package dao;

import bean.Announcement;
import util.Linksql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class announcementDaoImpl{
//    //查询所有数据库数据：
public List<Announcement> getAll() {
    Connection connection = Linksql.getConnection();
    String sql = "select * from announcement;";
    List list = new ArrayList<>();
    try{
        PreparedStatement prep = connection.prepareStatement(sql);
        prep.execute();
        ResultSet resultSet = prep.executeQuery();
        while (resultSet.next()){
            Announcement anno = new Announcement();
            anno.setAnnouncement_title(resultSet.getString("announcement_title"));
            anno.setAnnouncement_num(resultSet.getString("announcement_num"));
            anno.setContent(resultSet.getString("content"));
//            anno.setTime(resultSet.getString("fnum"));
            list.add(anno);
        }
//        for(int i=0;i< list.size();i++)
//        {System.out.println(list.get(i).getAnnouncement_num());
//            System.out.println(list.get(i).getAnnouncement_title());
//            System.out.println(list.get(i).getContent());
//
//        }
        prep.close();
        connection.close();
    } catch (SQLException throwables){
        throwables.printStackTrace();
    }
    return list;
}
    public void insert(String announcement_title,String content,String time) throws SQLException {
         Connection connection = Linksql.getConnection();
        String sql = "insert into announcement(announcement_title,content,time) values(?,?,?);";
        PreparedStatement prep = connection.prepareStatement(sql);
        prep.setString(1,announcement_title);
        prep.setString(2,content);
        prep.setString(3,time);
        prep.execute();
        prep.close();
        connection.close();
    }
    public void delete(String announcement_num) throws SQLException {
        Connection connection = Linksql.getConnection();
        String sql = "delete from announcement where announcement_num="+announcement_num;
        PreparedStatement prep = connection.prepareStatement(sql);
        prep.execute();
        prep.close();
        connection.close();
    }
}
